package org.marsdonne.commons.jpa;

import org.springframework.data.jpa.domain.Specification;

public interface FilterSpecSupport<T> {
    default Specification<T> by(String field, BasedFieldFilter fieldFilter) {
        return (root, query, builder) -> fieldFilter.generateCriteria(builder, root.get(field));
    }

    default Specification<T> by(String field, Object value) {
        return (root, query, builder) -> builder.equal(root.get(field), value);
    }

    default Specification<T> by(String joinedField, String nestedField, Object value) {
        return (root, query, builder) -> builder.equal(root.join(joinedField).get(nestedField), value);
    }
}
